<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\Request;
use think\exception\ValidateException;
class Login
{
    public function login(Request $request){
        $error='';
        if($request->isPost()){
            $data=$request->post();
//            dd($data);
            try {
                validate(\app\admin\validate\Login::class)->check($data);
                $username=\app\admin\model\Login::where('username',$data['username'])->find();
                if(!$username){
                    abort(404,'用户名错误');
                }else{
                    $password=$data['password'];
                    if($password != $username['password']){
                        abort(404,'密码错误');
                    }else{
                        session('adminlogin',$username->toArray());
                        return redirect(url('admin/index/show')->build());
                    }
                }
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                $error=$e->getError();
            } catch (\Exception $e){
                $error=$e->getMessage();
            }

        }
        return view('login',compact('error'));
    }

}
